<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>
            <div class="form-group">
                <label class="col-md-2 control-label">工资时间</label>
                <div class="col-md-10" style="width: 200px">
                    <input type="text" class="form-control" placeholder="工资时间" id="wageTime" name="wageTime"
                           data-bv-notempty="true" data-bv-notempty-message="工资时间不能为空">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">员工</label>
                <div class="col-md-10" style="width: 300px">
                    <select class="form-control input-sm" name="userId" id="userId"
                            data-bv-notempty="true" data-bv-notempty-message="员工不能为空">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">出勤天数</label>
                <div class="col-md-10" style="width: 200px">
                    <input class='form-control' type='text' id="attendanceDays" disabled="disabled"/>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>请假天数</label>
                <div class='col-md-10' style="width: 200px">
                    <input class='form-control' type='text' id="leaveDays" disabled="disabled"/>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>缺席天数</label>
                <div class='col-md-10' style="width: 200px">
                    <input class='form-control' type='text' id="absenceDays" disabled="disabled"/>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>基本工资</label>
                <div class='col-md-10' style="width: 200px">
                    <input class='form-control' type='text' id="basicSalary" disabled="disabled"/>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>奖金</label>
                <div class='col-md-10' style="width: 200px">
                    <input class='form-control' placeholder='奖金' type='text' name='bonus' id='bonus'/>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>罚款</label>
                <div class='col-md-10' style="width: 200px">
                    <input class='form-control' placeholder='罚款' type='text' name='fines' id='fines'/>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>提成</label>
                <div class='col-md-10' style="width: 200px">
                    <input class='form-control' placeholder='提成' type='text' name='commission' id='commission'/>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-2 control-label'>实际工资</label>
                <div class='col-md-10' style="width: 200px">
                    <input class='form-control' type='text' id="actualSalary" disabled="disabled"/>
                </div>
            </div>
            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <input type="hidden" id="id" name="id"/>
                        <button class="btn btn-primary" onclick="location.href='salaryList.html'">返回</button>
                        <button class="btn btn-primary salary_btn" type="submit" onclick="calcSalary()">
                            <i class="fa fa-save"></i> 计算工资
                        </button>
                        <button class="btn btn-primary salary_btn" type="submit" onclick="add()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>
</div>
<script type="text/javascript" src="../../js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap/bootstrap-select.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/my/roles/role.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/dict.js"></script>
<script type="text/javascript" src="../../js/my/queryData.js?v=2"></script>
<script type="text/javascript">
    layui.use(['layer', 'laydate', 'upload'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#wageTime',
            type: 'month',
            value: new Date(),
        });


        initEmps("userId");
        initSalary($("#userId").val());
    });

    $('#form').bootstrapValidator();

    function add() {
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }
        var formdata = $("#form").serializeObject();
        var id = $("#id").val();
        if (id && id != "") {
            if (!confirm("该员工已计算过工资，是否重新计算？")) {
                $(".salary_btn").attr("disabled", false);
                return;
            }
        }
        $.ajax({
            type: 'post',
            url: '/empSalary',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (data) {
                layer.msg("操作成功", {shift: -1, time: 1000}, function () {
                    location.href = "salaryList.html";
                });
            }
        });
    }

    $("#userId").on("change", function () {
        salaryReset();
        initSalary($(this).val());
    });

    function initSalary(userId) {
        var wageTime = $("#wageTime").val();
        if (!wageTime || wageTime == "") {
            layer.alert("请选择工资时间", {icon : 2});
            return;
        }
        var formdata = $("#form").serializeObject();
        $.ajax({
            type: 'post',
            url: '/empSalary/init',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (response) {
                if (response.code == "2000") {
                    var salary = response.data;
                    $("#attendanceDays").val(salary.attendanceDays);
                    $("#leaveDays").val(salary.leaveDays);
                    $("#absenceDays").val(salary.absenceDays);
                    $("#actualSalary").val(salary.actualSalary);
                    $("#basicSalary").val(salary.basicSalary);
                    $("#bonus").val(salary.bonus);
                    $("#fines").val(salary.fines);
                    $("#commission").val(salary.commission);
                    $("#id").val(salary.id);
                } else {
                    layer.alert("初始化工资失败", {icon : 2});
                }
                $(".salary_btn").attr("disabled", false);
            }
        });
    }

    function calcSalary() {
        initSalary($("#userId").val());
    }

    function salaryReset() {
        $("#attendanceDays").val("");
        $("#leaveDays").val("");
        $("#absenceDays").val("");
        $("#actualSalary").val("");
        $("#basicSalary").val("");
        $("#bonus").val("");
        $("#fines").val("");
        $("#commission").val("");
        $("#id").val("");
    }
</script>
</body>
</html>